package com.example.tyz1201.haha.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.example.tyz1201.haha.Adapter.ContentAdapter;
import com.example.tyz1201.haha.Model.Content;
import com.example.tyz1201.haha.Model.Data;
import com.example.tyz1201.haha.R;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lv_content;
    private String data = "";
    private String line = "";
    private Content content;
    private ContentAdapter contentAdapter;
    private List<Data> datas = new ArrayList<>();
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                getDataFromSomewhere();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lv_content = (ListView) findViewById(R.id.lv_content);
                        Gson gson = new Gson();
                        content = gson.fromJson(data, Content.class);

                        datas = content.getResult().getData();
                        Log.d("hehe", datas.get(0).getName1());

                        Data data = datas.get(0);
                        list.add(data.getName1());
                        list.add(data.getName2());
                        list.add(data.getName3());
                        list.add(data.getName4());
                        list.add(data.getName5());
                        list.add(data.getName6());
                        list.add(data.getName7());
                        list.add(data.getName8());
                        list.add(data.getName9());
                        list.add(data.getName10());
                        list.add(data.getName11());
                        list.add(data.getName12());
                        list.add(data.getName13());
                        list.add(data.getName14());
                        list.add(data.getName15());
                        list.add(data.getName16());
                        list.add(data.getName17());
                        list.add(data.getName18());
                        list.add(data.getName19());
                        list.add(data.getName20());
                        list.add(data.getName21());
                        list.add(data.getName22());
                        list.add(data.getName23());
                        list.add(data.getName24());
                        list.add(data.getName25());
                        list.add(data.getName26());
                        list.add(data.getName27());
                        list.add(data.getName28());
                        list.add(data.getName29());
                        list.add(data.getName30());
                        list.add(data.getName31());
                        list.add(data.getName32());
                        list.add(data.getName33());

                        contentAdapter = new ContentAdapter(list, MainActivity.this);
                        lv_content.setAdapter(contentAdapter);

                        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(MainActivity.this, InflateActivity.class);
                                intent.putExtra("position", (position + 1));

                                startActivity(intent);
                            }
                        });

                    }

                });

            }

        }).start();
    }


    private void getDataFromSomewhere() {
        try {
            URL url = new URL("http://japi.juhe.cn/funny/type.from?key=9137b9d52f086cbe85101d66fdd9fdb9");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);


            while ((line = br.readLine()) != null) {
                data = line + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
